package com.kin207.netty.spring.listener;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.kin207.netty.listener.Listener;
import com.kin207.netty.listener.ListenerConfig;
import com.kin207.netty.server.IServer;

public class SpringRegisterServerListener implements Listener<IServer> {
	final static Logger logger = LoggerFactory.getLogger(SpringRegisterServerListener.class);
	
	private String springApplicationContextPath = "spring/applicationContext.xml";
	private ApplicationContext applicationContext;
	
	public void initialize(ListenerConfig cfg) {
		logger.info("cfg:{}", cfg);
		if(cfg==null){
			return;
		}
		
		String path = cfg.getParam("springApplicationContextPath");
		if(path!=null && !"".equals(path)){
			logger.info("set springApplicationContextPath {}", path);
			springApplicationContextPath = path;
		}
		
		if(applicationContext==null){
			logger.info("init spring ApplicationContext for path {}", springApplicationContextPath);
			applicationContext =  new ClassPathXmlApplicationContext(springApplicationContextPath);
		}else{
			logger.info("spring exits, ApplicationContext for path {}", springApplicationContextPath);
		}
		
	}

	public synchronized void created(IServer server) {
		
	}

	public void destroyed(IServer server) {
		
	}

	public String getSpringApplicationContextPath() {
		return springApplicationContextPath;
	}

	public void setSpringApplicationContextPath(String springApplicationContextPath) {
		this.springApplicationContextPath = springApplicationContextPath;
	}

	
}
